﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace Elementum
{
    public class Music
    {
        SoundEffect _song;
        SoundEffectInstance _songInstance;
        float _fadeInterval;

        protected int _deltaTime=500;
        protected int _frameTime;

        public float Volume { set { _songInstance.Volume = value; }
            get { return _songInstance.Volume; }
        }

        //start with full volumn.
        public Music(String FromUrl, float interval,bool looped)
        {
            _song = ResourceManager.LoadSoundEffect(FromUrl);
            _songInstance = _song.CreateInstance();
            _songInstance.IsLooped = looped;
            _songInstance.Volume = 1.0f;
            _fadeInterval = interval;
        }
        //choose the volumn by yourself.
        public Music(String FromUrl, float interval, float volumn)
        {
            _song = ResourceManager.LoadSoundEffect(FromUrl);
            _songInstance = _song.CreateInstance();
            _songInstance.IsLooped = true;
            _songInstance.Volume = volumn;
            _fadeInterval = interval;
        }
        //function to play.
        public void MusicPlay()
        {
            _songInstance.Play();
        }
        //function to pause.
        public void MusicPause()
        {
            _songInstance.Pause();
        }
        //function to stop.
        public void MusicStop()
        {
            _songInstance.Stop();
            //_songInstance.
        }
        //function to free the memory space.
        public void MusicFree()
        {
            _songInstance.Stop();
            _songInstance.Dispose();
            while (!_songInstance.IsDisposed) { }
        }
        //fade out the music.
        public void MusicVolumnFadeOut()
        {
            MusicPlay();

            _frameTime += 1000000 / 60;
              if (_frameTime > _deltaTime)
              {
                  _frameTime -= _deltaTime;
                  if (_songInstance.Volume - _fadeInterval > 0.0f)
                  {
                      _songInstance.Volume -= _fadeInterval;
                  }
                  else
                  {
                      _songInstance.Volume = 0.0f;
                      //MusicStop();
                  }
              }
            
        }
        //fade in the music.
        public void MusicVolumnFadeIn()
        {
            //_songInstance.Volume = 0;
            MusicPlay();

             _frameTime += 1000000/60;
             if (_frameTime > _deltaTime)
             {
                 _frameTime -= _deltaTime;

                 if (_songInstance.Volume + _fadeInterval < 1.0f)
                 {
                     _songInstance.Volume += _fadeInterval;
                 }
                 else
                 {
                     _songInstance.Volume = 1.0f;
                 }
             }
        }
        //Set the repeat bool value.
        public bool Repeat
        {
            set 
            {
                _songInstance.IsLooped = value;
            }
            get
            {
                return _songInstance.IsLooped;
            }
        }

        
    }
}
